package org.jeecg.modules.demo.roomapartdistribute.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfApply;
import org.jeecg.modules.demo.applyinfo.mapper.ZnzfApplyMapper;
import org.jeecg.modules.demo.projectroom.entity.ZnzfRoom;
import org.jeecg.modules.demo.projectroom.mapper.ZnzfRoomMapper;
import org.jeecg.modules.demo.roomapartdistribute.entity.ZnzfCheckApart;
import org.jeecg.modules.demo.roomapartdistribute.entity.ZnzfCheckApartRoomList;
import org.jeecg.modules.demo.roomapartdistribute.entity.ZnzfRoomApartDistribute;
import org.jeecg.modules.demo.roomapartdistribute.mapper.ZnzfCheckApartMapper;
import org.jeecg.modules.demo.roomapartdistribute.mapper.ZnzfCheckApartRoomListMapper;
import org.jeecg.modules.demo.roomapartdistribute.mapper.ZnzfRoomApartDistributeMapper;
import org.jeecg.modules.demo.roomapartdistribute.service.IZnzfRoomApartDistributeService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 房源单位分配
 * @Author: jeecg-boot
 * @Date:   2022-03-18
 * @Version: V1.0
 */
@Service
public class ZnzfRoomApartDistributeServiceImpl extends ServiceImpl<ZnzfRoomApartDistributeMapper, ZnzfRoomApartDistribute> implements IZnzfRoomApartDistributeService {
    @Resource
    private ZnzfRoomApartDistributeMapper znzfRoomApartDistributeMapper;

    @Resource
    private ZnzfApplyMapper znzfApplyMapper;

    @Resource
    private ZnzfRoomMapper znzfRoomMapper;

    @Resource
    private ZnzfCheckApartMapper znzfCheckApartMapper;

    @Resource
    private ZnzfCheckApartRoomListMapper znzfCheckApartRoomListMapper;

    @Override
    public List<ZnzfRoom> searchRoomListByRadId(String radId) {
        //获取报名表id
        String apply_id = znzfRoomApartDistributeMapper.selectById(radId).getApplyInfo();
        //获取项目表id
        String project_id = znzfApplyMapper.selectById(apply_id).getProject();
        //获取所有跟项目表id一致的房源信息
        LambdaQueryWrapper<ZnzfRoom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfRoom::getProjectId, project_id);
        List<ZnzfRoom> znzfRoomList = znzfRoomMapper.selectList(queryWrapper);
        if (znzfRoomList == null)
            return null;
        return znzfRoomList;
    }

    /**
     * 判断分给单位的房源数据是不是等于房源列表表中的数量
     *
     * @param checkApartId
     * @return
     */
    @Override
    public boolean roomToDepartEqual(String checkApartId) {
        ZnzfCheckApart checkApart = znzfCheckApartMapper.selectById(checkApartId);
        LambdaQueryWrapper<ZnzfCheckApartRoomList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnzfCheckApartRoomList::getCheckApart,checkApartId);
        List<ZnzfCheckApartRoomList> roomLists = znzfCheckApartRoomListMapper.selectList(wrapper);
        return checkApart.getCheckedRoomNum() == roomLists.size();
    }
}
